var template = require('art-template')
const path = require('path');
const fs = require('fs');
const { dataArray } = require('../model/list');
const jwt = require('jsonwebtoken');

const list = (req, res) => {
    var html = template(path.join(__dirname, '../view/list-html.art'), {
        data: dataArray
    });
    fs.writeFileSync(path.join(__dirname, '../public/list.html'), html)
    res.send(html);
};

const token = (req, res) => {

    // 1.对称加密
    // let tk = jwt.sign({ username: 'admin1' }, 'love'); // 加密
    // const result = jwt.verify(tk, 'love') // 解密   love-秘钥
    // res.send(result);


    // 2.非对称加密
    const privateKey = fs.readFileSync('./key/rsa_private_key.pem'); // 获取 私钥
    const tk = jwt.sign({ username: 'admin2' }, privateKey, { algorithm: 'RS256' }); // 加密私钥
    console.log('tk--', tk);
    console.log('----------');

    const publickey = fs.readFileSync(path.join(__dirname, '../key/rsa_public_key.pem'), { algorithm: 'RS256' }); // 获取 公钥
    console.log('publickey--', publickey);

    // 解密 (token,公钥)
    const result = jwt.verify(tk, publickey);
    console.log('result:', result);

    /* 
    1.本地生成一个私钥
    2.本地生成一个公钥 token
    3.把token传给geithub
    4.在github后台把公钥后台设置好
    5.拿到公钥可以做验证了(不用输入用户名和密码)
    
     */

    res.send(result);
}


exports.list = list;
exports.token = token;